package com.hamster.tools.i18nManger.view
{
	import com.hamster.tools.i18nManger.events.AppEvent;
	import com.hamster.tools.i18nManger.model.ConfigurationVOProxy;
	import com.hamster.tools.i18nManger.model.TranslateConfVOProxy;
	import com.hamster.tools.i18nManger.vo.ConfigurationVO;
	import com.hamster.tools.i18nManger.vo.TranslateConfVO;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class PropertyEditorViewMediator extends Mediator
	{
		public static const NAME:String = "PropertyEditorViewMediator";
		
		public function PropertyEditorViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			if (configurationVO.autoTranlate == "Y") {
				if (translateConfVO.expires_in + translateConfVO.request_time < new Date().time / 1000) {
					this.sendNotification(AppEvent.TRANSLATE_ACCESS_TOKEN_REQUEST);
				} else {
					handleTranslateAccessTokenResponse(null);
				}
			}
		}
		
		override public function onRemove():void
		{
			
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				AppEvent.TRANSLATE_ACCESS_TOKEN_RESPONSE,
				AppEvent.TRANSLATE_ACCESS_TOKEN_FAULT,
				AppEvent.TRANSLATE_RESPONSE,
				AppEvent.TRANSLATE_FAULT,
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			
			switch (name) {
				case AppEvent.TRANSLATE_ACCESS_TOKEN_RESPONSE:
					handleTranslateAccessTokenResponse(body);
					break;
				case AppEvent.TRANSLATE_ACCESS_TOKEN_FAULT:
					handleTranslateAccessTokenFault(body);
					break;
				case AppEvent.TRANSLATE_RESPONSE:
					handleTranslateResponse(body);
					break;
				case AppEvent.TRANSLATE_FAULT:
					handleTranslateFault(body);
					break;
			}
		}
		
		private function handleTranslateAccessTokenResponse(data:Object):void
		{
			var translateObj:Object = {
				text : app.itemVO.value,
				from : app.itemVO.language,
				to   : app.targetLanguage
			};
			this.sendNotification(AppEvent.TRANSLATE_REQUEST, translateObj);
		}
		
		private function handleTranslateAccessTokenFault(data:Object):void
		{
			app.autoTransStatus.text = "Translate Service not available.";
		}
		
		private function handleTranslateResponse(data:Object):void
		{
			app.autoTransStatus.text = "Translate Success.";
			app.translatedText.text = data.toString();
		}
		
		private function handleTranslateFault(data:Object):void
		{
			app.autoTransStatus.text = "Translate Failed.";
		}
		
		
		public function get app():PropertyEditorView
		{
			return viewComponent as PropertyEditorView;
		}
		
		public function get translateConfVO():TranslateConfVO
		{
			return (facade.retrieveProxy(TranslateConfVOProxy.NAME) as TranslateConfVOProxy).translateConfVO;
		}
		
		public function get configurationVO():ConfigurationVO
		{
			return (facade.retrieveProxy(ConfigurationVOProxy.NAME) as ConfigurationVOProxy).configurationVO;
		}
	}
}